#pragma once
#include "noncopyable.h"

#include <functional>
#include <string>
#include <vector>
#include <memory>
//作用：创建多个eventloop和thread绑定的对象

class EventLoop;
class EventLoopThread;


class EventLoopThreadPool : noncopyable {
    public:
        using ThreadInitCallback = std::function<void(EventLoop *)>;
        EventLoopThreadPool(EventLoop *baseLoop, const std::string &nameArg);
        ~EventLoopThreadPool();

        void setThreadNum(int numThreads) { numThreads_ = numThreads; }
        void start(const ThreadInitCallback& cb = ThreadInitCallback());

        // 如果工作在多线程中，baseLoop_会默认以轮询的方式分配channel给subloop
        EventLoop *getNextLoop();
        
        std::vector<EventLoop*> getAllLoops();
        
        bool started() const { return started_; }
        const std::string& name() { return name_; }

    private:
        EventLoop *baseLoop_;//在用户代码中创建的eventloop对象，即main reactor
        std::string name_;
        bool started_;
        int numThreads_;
        int next_;

        std::vector<std::unique_ptr<EventLoopThread>> threads_;
        std::vector<EventLoop *> loops_;

};
